package com.qidianit.mes.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.common.Result;
import com.qidianit.mes.pojo.Supplier;
import com.qidianit.mes.sys.mapper.SupplierMapper;
import com.qidianit.mes.sys.service.SupplierService;
import com.qidianit.mes.vo.SupplierPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Package Path: com.qidianit.mes.sys.service.impl
 * Author: Alhz
 * Description: 供应商
 * Date: 2025/9/16 19:32
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;


    /**
     * 分页查询供应商信息
     * @param supplierPageVo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result<PageInfo<Supplier>> findAll(SupplierPageVo supplierPageVo, int pageNum, int pageSize) {

        //加载分页
        PageHelper.startPage(pageNum, pageSize);
        //查询供应商信息
        List<Supplier> supplierList = supplierMapper.selectAllSupplier(supplierPageVo);

        PageInfo<Supplier> pageInfo = new PageInfo<>(supplierList);

        return Result.success(pageInfo);
    }

    /**
     * 根据供应商code查询对应信息
     * @param code
     * @return
     */
    @Override
    public Result selectSupplierByCode(String code) {
        return Result.success(supplierMapper.selectSupplierByCode(code));
    }


    /**
     * 添加供应商信息
     * @param supplier
     * @return
     */
    @Override
    public Result insertSupplier(Supplier supplier) {
        //更新当前用户的创建与更新时间
        supplier.setCreateTime(LocalDateTime.now());
        supplier.setUpdateTime(LocalDateTime.now());

        //判断供应商是否存在
        if(supplierMapper.selectSupplierByCode(supplier.getCode())!=null){
            return Result.error("用户已经存在,操作失败");
        }
        supplierMapper.insertSupplier(supplier);

        return Result.success();
    }

    /**
     * 更新供应商信息
     * @param supplier
     * @return
     */
    @Override
    public Result updateSupplier(Supplier supplier) {
        supplier.setUpdateTime(LocalDateTime.now());

        Supplier supplier1 = supplierMapper.selectByID(supplier.getId());

        if(supplier1.getCode().equals(supplier.getCode())){
            supplierMapper.updateSupplier(supplier);
        }
        else {
            if(supplierMapper.selectSupplierByCode(supplier.getCode())!=null){
                return Result.error("供应商编号已经存在，操作失败");
            }
            supplierMapper.updateSupplier(supplier);
        }

        return Result.success();
    }

    @Override
    public Result deleteSupplierByID(Long id) {
        if(supplierMapper.selectByID(id)==null){
            return Result.error("供应商不存在,操作失败");
        }
        supplierMapper.deleteSupplierByID(id);
        return Result.success();
    }


    @Override
    public Result batchDeleteSupplier(Long[] ids) {
        return supplierMapper.batchDeleteSupplier(ids)>0?Result.success():Result.error("操作失败");
    }
}
